//
//  CYYEditMyPartyViewController.m
//  LetsParty
//
//  Created by rimi on 15/9/15.
//  Copyright (c) 2015年 LetsPartyTeam. All rights reserved.
//

#import "CYYEditMyPartyViewController.h"
#define TEXTFIELD(_tag) ((UITextField *)[super.view viewWithTag:_tag])
#define CONTROLLER(_tag) [super.view viewWithTag:_tag]
#define TEXTVIEW_TEXT(_tag) ((UITextView *)[self.view viewWithTag:_tag]).text
@interface CYYEditMyPartyViewController ()

@property (nonatomic, strong) UIButton * updateButton;//更新按钮
@property (nonatomic, strong) UIButton * deleteButton;//取消活动按钮
@property (nonatomic, strong) UIAlertController * backAlterViewController;//弹出框

- (void)initUserInterface;//子类初始化界面方法

@end

@implementation CYYEditMyPartyViewController
#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
}
#pragma mark - responds to event
- (void)respondsToUpdateButton:(UIButton *)button{
//    NSLog(@"点击第一次");
    button.userInteractionEnabled = NO;
    [self editMyParty];
}

- (void)respondsToDeleteButton:(UIButton *)button{
    __weak typeof(self) myVC = self;
    button.userInteractionEnabled = NO;
    [CYYNetWorkManager deleteSponsorPartyWithObjectId:self.partyObjectId sucess:^(NSString *sucessString) {
        //返回主界面
        [self.navigationController popToViewController:[[ControllerManager sharedManager] mainTabController] animated:YES];
        _backAlterViewController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"活动删除成功" preferredStyle:UIAlertControllerStyleAlert];
        [_backAlterViewController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            [_backAlterViewController dismissViewControllerAnimated:YES completion:nil];
        }]];
        [myVC.parentViewController presentViewController:_backAlterViewController animated:YES completion:nil];
        button.userInteractionEnabled = YES;
    } fail:^(NSError *error) {
        button.userInteractionEnabled = YES;
    }];
}

- (void)respondsToLeftButton:(UIButton *)button{
    [[ControllerManager sharedManager].rootViewController popViewControllerAnimated:YES];
}
#pragma mark - private methods
- (void)initUserInterface{
    [self.leftNavButton setImage:[UIImage imageNamed:@"iconfont-zuojiantou"] forState:UIControlStateNormal];
    [self.leftNavButton addTarget:self action:@selector(respondsToLeftButton:) forControlEvents:UIControlEventTouchUpInside];
    //取到父类的暗号相关控件
    CONTROLLER(titleLabelTag).hidden = YES;
    CONTROLLER(partySecretCodeContentTextViewTag).hidden = YES;
    CONTROLLER(checkButtonTag).hidden = YES;
    CONTROLLER(autoCreateButtonTag).hidden = YES;
    CONTROLLER(sponsorButtonTag).hidden = YES;
    CONTROLLER(partyDetailTextContentTextViewTag).frame = DHFlexibleFrame(CGRectMake(85, 206, 210, 150), NO);
    TEXTFIELD(partyTitleContentTextViewTag).text = self.partyTitleString;
    TEXTFIELD(partyBeginTimeContentTextViewTag).text = self.partyBeginTimeString;
    TEXTFIELD(partyEndTimeContentTextViewTag).text = self.partyEndTimeString;
    TEXTFIELD(partyBeginTimeContentTextViewTag).font = [UIFont systemFontOfSize:14];
    TEXTFIELD(partyEndTimeContentTextViewTag).font = [UIFont systemFontOfSize:14];
    TEXTFIELD(partyEndTimeContentTextViewTag).text = self.partyEndTimeString;
    TEXTFIELD(partyPositionNameContentTextViewTag).text = self.partyPositionNameString;
    TEXTVIEW_TEXT(partyDetailTextContentViewTextViewTag) = self.partyDetailString;
    TEXTFIELD(partyDetailTextContentTextViewTag).hidden = YES;
    [self.mainScrollView addSubview:self.updateButton];
    [self.updateButton addTarget:self action:@selector(respondsToUpdateButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.mainScrollView addSubview:self.deleteButton];
    [self.deleteButton addTarget:self action:@selector(respondsToDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
    
}
//编辑我的活动
- (void)editMyParty{
    if ([TEXTFIELD(partyBeginTimeContentTextViewTag).text compare:TEXTFIELD(partyEndTimeContentTextViewTag).text] == 1) {
        [self alterViewWithMessage:@"活动开始时间不能大于结束时间"];
        self.updateButton.userInteractionEnabled = YES;
//        NSLog(@"点击第2次");
        return;
    }
    if ([TEXTFIELD(partyBeginTimeContentTextViewTag).text compare:[CYYNetWorkManager tStringFromDate:[NSDate date]]] == -1) {
        [self alterViewWithMessage:@"活动时间不能小于当前时间"];
        self.updateButton.userInteractionEnabled = YES;
//        NSLog(@"点击第2次");
        return;
    }
    __weak typeof(self) myVC = self;
    [CYYNetWorkManager editMySponsorPartyWithPartyId:self.partyObjectId
                                          partyTitle:TEXTFIELD(partyTitleContentTextViewTag).text
                                      partyBeginTime:TEXTFIELD(partyBeginTimeContentTextViewTag).text
                                        partyEndTime:TEXTFIELD(partyEndTimeContentTextViewTag).text
                                   partyPositionName:TEXTFIELD(partyPositionNameContentTextViewTag).text
                             partyPositionCoordinate:nil
                                     partyDetailText:TEXTVIEW_TEXT(partyDetailTextContentViewTextViewTag)
                                              sucess:^(NSString *sucessString) {
                                                  //返回详情界面
                                                  [myVC respondsToLeftButton:myVC.leftNavButton];
                                                  _backAlterViewController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"信息更新成功" preferredStyle:UIAlertControllerStyleAlert];
                                                  [_backAlterViewController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                                                      [_backAlterViewController dismissViewControllerAnimated:YES completion:nil];
                                                  }]];
                                                  [myVC.parentViewController presentViewController:_backAlterViewController animated:YES completion:nil];
                                                  self.updateButton.userInteractionEnabled = YES;
//                                                  NSLog(@"点击第2次");
                                              }
                                                fail:^(NSError *error) {
//                                                    NSLog(@"%@",error);
                                                    [myVC alterViewWithMessage:@"网络出错"];
                                                    self.updateButton.userInteractionEnabled = YES;
                                                }];
}
#pragma mark - getter
- (UIButton *)updateButton{
    if (!_updateButton) {
        _updateButton = ({
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = DHFlexibleFrame(CGRectMake(200, 400, 90, 30), NO);
            [button setTitle:@"更新" forState:UIControlStateNormal];
            button.backgroundColor = COLOR_TagBlue;
            button;
        });
    }
    return _updateButton;
}

- (UIButton *)deleteButton{
    if (!_deleteButton) {
        _deleteButton = ({
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = DHFlexibleFrame(CGRectMake(30, 400, 90, 30), NO);
            [button setTitle:@"取消活动" forState:UIControlStateNormal];
            button.backgroundColor = [UIColor redColor];
            button;
        });
    }
    return _deleteButton;
}

@end
